export default {
    namespaced: true,
    state: {
        type: 'all',
        list: JSON.parse(localStorage.getItem('list')) || []
    },
    mutations: {
        //1.修改复选框isDone的值
        isChange(state, id) {
            const obj = state.list.find(item => item.id === id)
            obj.isDone = !obj.isDone
        },
        //2.删除任务
        delList(state, id) {
            state.list = state.list.filter(item => item.id !== id)
        },
        //3.新增任务
        addList(state, content) {
            state.list.push({
                id: +new Date(),
                name: content,
                isDone: false,
            })
        },
        //4.全选功能
        checkFn(state, check) {
            state.list.forEach(item => item.isDone = check)
        },
        //7-2.清除按钮
        clear(state) {
            state.list = state.list.filter(item => !item.isDone)
        },
        //8.切换高亮
        changeType(state, type) {
            state.type = type
        }

    },
    actions: {
        //7-1.延时1秒清除已完成任务
        clearAsync(context) {
            setTimeout(() => {
                context.commit('clear')
            }, 1000)
        }
    },
    getters: {
        //5.显示未完成任务的数量
        length(state) {
            return state.list.filter(item => !item.isDone).length
        },
        //6.清除未完成任务的按钮的显示(计算是否有 isDone为fasle)
        isShow(state) {
            return state.list.some(item => !item.isDone)
        },
        //9.对应高亮现实的内容
        showList(state) {
            if (state.type === 'complete') {
                return state.list.filter(item => item.isDone)
            } else if (state.type === 'active') {
                return state.list.filter(item => !item.isDone)
            } else {
                return state.list
            }
        }

    },

}